Skip to content

Read source and config files as UTF-8 regardless of locale - #479

Open
sinsoku wants to merge 1 commit into
ruby:masterfrom
sinsoku:fix-file-read-encoding
Open

Read source and config files as UTF-8 regardless of locale#479
sinsoku wants to merge 1 commit into
ruby:masterfrom
sinsoku:fix-file-read-encoding

Conversation

@sinsoku

@sinsoku sinsoku commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

File.read without an encoding follows Encoding.default_external, which is US-ASCII when LANG is not set (e.g., ruby-lsp started from a GUI editor). With a .rbs file containing non-ASCII characters, RBS::Parser then raises ArgumentError (invalid byte sequence in US-ASCII). This is not a RBS::ParsingError and is not rescued, so one such file aborts add_workspace and batch entirely. typeprof.conf.jsonc has the same problem.

Ruby files are mostly fine because Prism ignores the encoding of the source string. They still fail when Encoding.default_internal is set (e.g., ruby -U), since File.read then transcodes from US-ASCII. So this reads them as UTF-8 too.

The rbs gem has read signature files as UTF-8 for the same reason since ruby/rbs#493.

Verification

  • CI passes.
  • env -u LANG bundle exec rake test passes. Before the fix, the new tests fail with ArgumentError: invalid byte sequence in US-ASCII.

🤖 Generated with Claude Code

`File.read` without an encoding follows `Encoding.default_external`,
which is US-ASCII when `LANG` is not set (e.g., ruby-lsp started from a
GUI editor). With a `.rbs` file containing non-ASCII characters,
`RBS::Parser` then raises `ArgumentError` (invalid byte sequence in
US-ASCII). This is not a `RBS::ParsingError` and is not rescued, so one
such file aborts `add_workspace` and `batch` entirely.
`typeprof.conf.jsonc` has the same problem.

Ruby files are mostly fine because Prism ignores the encoding of the
source string. They still fail when `Encoding.default_internal` is set
(e.g., `ruby -U`), since `File.read` then transcodes from US-ASCII. So
this reads them as UTF-8 too.

The rbs gem has read signature files as UTF-8 for the same reason since
ruby/rbs#493.
sinsoku added a commit to sinsoku/ruby-lsp-typeprof that referenced this pull request Sep 9, 2026
TypeProf reads source and config files with `File.read`, which follows
`Encoding.default_external`. When `LANG` is not set (e.g. ruby-lsp
launched from a GUI editor), that is US-ASCII and a non-ASCII `.rbs`
file raises `ArgumentError` in `add_workspace`. `activate` rescued it,
so code lens and document symbol silently disappeared.

Changing `Encoding.default_external` is process-wide, but ruby-lsp runs
add-on hooks sequentially on a single worker thread, so no other add-on
runs while it is temporarily switched.

TypeProf will read files as UTF-8 itself once ruby/typeprof#479 is
released, at which point this workaround can be dropped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant